<?php

class Log_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = 'log';
        $this->load->model("functions_model");
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function newLog($action, $type, $description, $user="System") {
        $data['user'] = $user;
        $data['action'] = $action;
        $data['type'] = $type;
        $data['description'] = $description;
        $data['date_added'] = date("Y-m-d H:i:s");
        $this->db->insert($this->tableName, $data);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $result[] = $row;
        }
        return $result;
    }

//    function getAll($username) {
//       // $data['added_by'] = $username;
//        $this->db->select('tid,bid_no,company,where_advertised,date_advertised,description,cost,date_added');
//        //$this->db->where($data);
//        $this->db->from($this->tableName);
//        $this->db->order_by("tid", "desc");
//        $query = $this->db->get();
//        $result = array();
//        foreach ($query->result_array() as $row) {
//            $row['date_advertised'] = $this->functions_model->prettyDate($row['date_advertised']);
//            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
//            $row['more'] = "<a href='javascript:loadUrl(\"#bids_result\",\"" . base_url() . "index.php/more_info/bidsMoreInfo/" . $row['tid'] . "\")'>More details</a>";
//
//            unset($row['tid']);
//            $result[] = $row;
//        }
//        return $result;
//    }

    function adminGetAll($field=NULL, $search=NULL) {
//        sleep(2);
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("user", $search);
                $this->db->or_like("action", $search);
                $this->db->or_like("type", $search);
                $this->db->or_like("description", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select('tid,action, type, description,user,date_added');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            //   unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
